package com.ruoyi.web.controller.work;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.LhqTeachingMaterial;
import com.ruoyi.domain.LhqWork;
import com.ruoyi.domain.LhqWorkCategory;
import com.ruoyi.domain.YcPersonal;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.ILhqWorkCategoryService;
import com.ruoyi.service.ILhqWorkService;
import com.ruoyi.service.IYcPersonalService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 著作Controller
 *
 * @author ruoyi
 * @date 2020-09-08
 */
@Controller
@RequestMapping("/ruoyi/work")
public class LhqWorkController extends BaseController {
    private String prefix = "ruoyi/work";
    private final static String ALL="全部";
    @Autowired
    private ILhqWorkService lhqWorkService;
    @Resource
    private ILhqWorkCategoryService iLhqWorkCategoryService;
    @Resource
    private IYcPersonalService iYcPersonalService;

    @RequiresPermissions("ruoyi:work:view")
    @GetMapping()
    public String work() {
        return prefix + "/work";
    }

    /**
     * 查询著作列表
     */
    @RequiresPermissions("ruoyi:work:listadmin")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LhqWork lhqWork) {
        startPage();
        //ALL代表全部,如果搜索框内是全部就给传一个空值
        if (ALL.equals(lhqWork.getCategory())){
            lhqWork.setCategory(null);
            List<LhqWork> list = lhqWorkService.selectLhqWorkListGl(lhqWork);
            return getDataTable(list);
        }
        //查询数据
        List<LhqWork> list = lhqWorkService.selectLhqWorkListGl(lhqWork);
        return getDataTable(list);
    }

    /**
     * 查询著作类别
     */
    @GetMapping("/category")
    @ResponseBody
    public List<LhqWorkCategory> titleOfWork(LhqWorkCategory lhqWorkCategory) {
        return iLhqWorkCategoryService.selectLhqWorkCategoryList(lhqWorkCategory);
    }

    /**
     * 导出著作列表
     */
    @RequiresPermissions("ruoyi:work:export")
    @Log(title = "著作", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LhqWork lhqWork) {
        List<LhqWork> list = lhqWorkService.selectLhqWorkList(lhqWork);
        ExcelUtil<LhqWork> util = new ExcelUtil<LhqWork>(LhqWork.class);
        return util.exportExcel(list, "work");
    }

    /**
     * 新增著作
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存著作
     */
    @RequiresPermissions("ruoyi:work:add")
    @Log(title = "著作", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LhqWork lhqWork) {
        LhqWorkCategory lhqWorkCategory = iLhqWorkCategoryService.selectByName(lhqWork.getCategory());
        lhqWork.setApplicant(iYcPersonalService.selectYcPersonalById(ShiroUtils.getUserId()).getuName());
        lhqWork.setUid(iYcPersonalService.selectYcPersonalById(ShiroUtils.getUserId()).getuId());
        lhqWork.setNaturalEndowments(lhqWorkCategory.getNaturalEndowments());
        lhqWork.setReward(lhqWorkCategory.getReward());
        return toAjax(lhqWorkService.insertLhqWork(lhqWork));
    }

    /**
     * 修改著作
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        LhqWork lhqWork = lhqWorkService.selectLhqWorkById(id);
        mmap.put("lhqWork", lhqWork);
        return prefix + "/edit";
    }

    /**
     * 修改保存著作
     */
    @RequiresPermissions("ruoyi:work:edit")
    @Log(title = "著作", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LhqWork lhqWork) {
        LhqWorkCategory lhqWorkCategory = iLhqWorkCategoryService.selectByName(lhqWork.getCategory());
        lhqWork.setNaturalEndowments(lhqWorkCategory.getNaturalEndowments());
        lhqWork.setReward(lhqWorkCategory.getReward());
        return toAjax(lhqWorkService.updateLhqWork(lhqWork));
    }

    /**
     * 删除著作
     */
    @RequiresPermissions("ruoyi:work:remove")
    @Log(title = "著作", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(lhqWorkService.deleteLhqWorkByIds(ids));
    }

    /**
     * 通过
     */
    @RequiresPermissions("ruoyi:work:tongguo")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/tongguo")
    @ResponseBody
    public AjaxResult tongguo(Long id) {
        LhqWork lhqWork=new LhqWork();
        lhqWork.setId(id);
        lhqWork.setExamineApprove(3L);
        //更新审批状态为待审批
        lhqWorkService.updateLhqWork(lhqWork);
        return success();
    }
    /**
     * 驳回
     */
    @RequiresPermissions("ruoyi:work:bohui")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/bohui")
    @ResponseBody
    public AjaxResult bohui(Long id) {
        LhqWork lhqWork=new LhqWork();
        lhqWork.setId(id);
        lhqWork.setExamineApprove(2L);
        //更新审批状态为待审批
        lhqWorkService.updateLhqWork(lhqWork);
        return success();
    }
    /**
     * 备案驳回
     */
    @RequiresPermissions("ruoyi:work:beianbohui")
    @Log(title = "备案状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/beianbohui")
    @ResponseBody
    public AjaxResult beianbohui(Long id) {
        LhqWork lhqWork=new LhqWork();
        lhqWork.setId(id);
        lhqWork.setKeepOnRecord(3L);
        //更新审批状态为待审批
        lhqWorkService.updateLhqWork(lhqWork);
        return success();
    }
    /**
     * 备案
     */
    @RequiresPermissions("ruoyi:work:beian")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/beian")
    @ResponseBody
    public AjaxResult beian(Long id) {
        LhqWork lhqWork=new LhqWork();
        lhqWork.setId(id);
        lhqWork.setKeepOnRecord(1L);
        //更新审批状态为待审批
        lhqWorkService.updateLhqWork(lhqWork);
        return success();
    }
    /**
     *
     */
    @RequiresPermissions("system:dict:list")
    @GetMapping("/chakan/{uid}")
    public String chakan(@PathVariable("uid") Long uid, ModelMap mmap)
    {
        YcPersonal ycPersonal = iYcPersonalService.selectYcPersonalById(uid);
        System.out.println(ycPersonal);
        mmap.put("ycPersonal", ycPersonal);
        return prefix + "/particularss";
    }
}
